package test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class InsertBatch {
	public void saveResults(ArrayList<Object[]> resultList,String sql,Connection conn) {
		
		
	/*	DBUtils dao = new DBUtils();
		Connection conn = null; */
		PreparedStatement sm =null;
		ResultSet rs = null;
		try {
		// 加载驱动
	//	conn = dao.getConn();
		conn.setAutoCommit(false);
		
		
		sm = conn.prepareStatement(sql);
		//sm.setString(1, "");
		int index = 0;
		
		for (Object[] objects : resultList) {
			System.out.println();
			for (int i = 0; i < 6; i++) {
				System.out.print(objects[i]+"\t");
				sm.setObject(i + 1, objects[i]);
			}
			sm.addBatch();
			index++;
			
			if (index >= 1000) {// 每1000条数据进行一次批量导入

				// 批量插入数据
				sm.executeBatch();
				conn.commit();
				System.out.println("导入数据：1000条");
		
				sm = null;
				sm = conn.prepareStatement(sql);
				index = 0;
			}
		}
		// 批量插入数据
		sm.executeBatch();
		conn.commit();
		System.out.println("finish");
		
		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			//dao.close(conn, sm, rs);
		}
	}
}
